<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>
        后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <template>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" width="200" header-align="center"></el-table-column>
        <el-table-column prop="pinyin" label="拼音" width="200" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="description" label="简介" align="center"></el-table-column>
        <el-table-column prop="keywords" label="关键词列表" width="200" align="center"></el-table-column>
        <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
        <el-table-column label="是否启用" width="80" align="center">
          <template slot-scope="scope">
            <el-switch
                @change="toggleEnable(scope.row)"
                v-model="scope.row.enable"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#cccccc">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini"
                       @click="openEditDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini"
                       @click="openDeleteConfirm(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </div>
</template>

<script>
export default {
  data(){
    return{
      tableData: []
    }
  },
  methods: {
    toggleEnable(brand) {
      console.log('你点击了【' + brand.name + '】的开关，当前开关值为：' + brand.enable);
      let url = 'http://localhost:9080/brands/' + brand.id;
      brand.enable == 1 ? url += '/enable' : url += '/disable';
      console.log('url = ' + url);
      let enableContext = ['禁用', '启用']
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          let message = enableContext[brand.enable] + '品牌【' + brand.name + '】成功！';
          this.$message.success(message);
        } else if (responseBody.state == 40400) {
          this.$message.error(responseBody.message);
          this.loadBrandList();
        } else if (responseBody.state == 40900) {
          this.$message.error(responseBody.message);
        }
      })
    },
    openDeleteConfirm(brand){
      this.$confirm('此操作将永久删除【'+brand.name+'】品牌, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(brand);
      }).catch(() => {

      });
    },
    handleDelete(brand){
      let url = 'http://localhost:9080/brands/'+brand.id+'/deleteById';
      console.log('url = ' + url);
      let formData = 'id=' + brand.id;
      console.log('formData = ' + formData)
      this.axios.post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.loadBrandList();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
        }else {
          // this.$message.error(responseBody.message);
          this.$alert(responseBody.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadBrandList();
            }
          });
        }
      })
    },
    loadBrandList(){
      let url = 'http://localhost:9080/brands';
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      })
    }
  },
  mounted() {
    this.loadBrandList();
  }
}
</script>

<style scoped>

</style>